<?php
/*
	Copyright (c) InterAKT Online 2000-2005
*/

class XML_TableExport {
	var $columns;
	var $connection;
	var $table;
	var $sortByField;
	var $order;
	var $maxRecords;
	var $dbEncoding;
	var $xmlEncoding;
	var $xmlFormat;
	
	function XML_TableExport(&$connection) {
		$this->connection = $connection;
		$this->maxRecords = 'ALL';
		$this->xmlFormat = 'NODES';
		$this->columns = array();
		$this->sortByField = '';
	}
	
	function setTable($table) {
		$this->table = trim($table);
	}
	
	function setSortByField($sortByField) {
		$this->sortByField = trim($sortByField);
	}
	
	function setOrder($order) {
		$this->order = strtoupper(trim($order));
		if ($this->order != 'DESC') {
			$this->order = 'ASC';
		}
	}
	
	function setMaxRecords($maxRecords) {
		$this->maxRecords = trim($maxRecords);
	}
	
	function setDBEncoding($dbEncoding) {
		$this->dbEncoding = trim($dbEncoding);
	}
	
	function setXMLEncoding($xmlEncoding) {
		$this->xmlEncoding = trim($xmlEncoding);
	}
	
	function setXMLFormat($xmlFormat) {
		$this->xmlFormat = strtoupper(trim($xmlFormat));
		if ($this->xmlFormat != 'ATTRIBUTES') {
			$this->xmlFormat == 'NODES';
		}
	}
	
	function Execute($method, $reference) {
		$ret = KT_getRealValue($method, $reference);
		if (!isset($ret)) {
			return;
		}

		$sql = 'SELECT * FROM ' . $this->table;
		if ($this->sortByField != '') {
			$sql = 'SELECT * FROM ' . $this->table . ' ORDER BY ' . $this->sortByField . ' ' . $this->order;
		}
		$recordset = $this->connection->Execute($sql);
		if ($this->connection->ErrorMsg()) {
			die($this->connection->ErrorMsg() . '<br />' . $sql);
		}

		if ( !isset($recordset) || !isset($recordset->fields) ) {
			die('<strong>XML_Export Error.</strong><br/>Passed argument is not a valid recordset.');
			return;
		}
		
		if (is_a($this->connection, 'KT_Connection')) {
			if (is_array($recordset->fields)) {
				$this->columns = array_keys($recordset->fields);
			}
		} else {
			$res = $this->connection->MetaColumns($this->table);
			foreach($res as $field => $col) {
				$this->columns[] = $col->name;
			}
		}
		
		$trans = array_flip(get_html_translation_table(HTML_ENTITIES));
		$numRow = 1;
		$document = '';
		$document .= '<'.'?xml version="1.0" encoding="' . $this->xmlEncoding . '"?'.'>';
		$document .= "\r\n";
		$document .= '<' . $this->table . '>';
		$document .= "\r\n";
		while (!$recordset->EOF) {
			if ($this->maxRecords != 'ALL' && $this->maxRecords < $numRow) {
				break;
			}
			$row = "\t";
			$row .= '<row';
			if ($this->xmlFormat != 'ATTRIBUTES') {
				$row .= '>' . "\r\n";
			}
			foreach ($this->columns as $key => $column) {
				$value = $recordset->Fields($column);
				$value = strtr($value, $trans);
				
				if ($this->xmlEncoding != $this->dbEncoding) {
					if (!function_exists('mb_convert_encoding')) {
						die('<strong>XML_Export Error.</strong><br/>Could not perform characters conversion. Function <strong>mb_convert_encoding</strong> is not available! Please enable the <strong>mbstring</strong> extension!');
					}
					$value = mb_convert_encoding($value, $this->xmlEncoding, $this->dbEncoding);
				}
				$value = str_replace(array('&', '>', '<', '"'), array('&amp;', '&gt;', '&lt;', '&quot;'), $value);
				
				if ($this->xmlFormat == 'ATTRIBUTES') {
					$row .= ' ' . $column . '="';
					$row .= $value;
					$row .= '"';
				} else {
					$row .= "\t\t";
					$row .= '<' . $column . '>';
					$row .= $value;
					$row .= '</' . $column . '>';
					$row .= "\r\n";
				}
			}
			if ($this->xmlFormat == 'ATTRIBUTES') {
				$row .= ' />';
			} else {
				$row .= "\t" . '</row>';
			}
			$document .= $row;
			$document .= "\r\n";
			$numRow++;
			$recordset->MoveNext();
		}
		$document .= '</' . $this->table . '>';
		$size = strlen($document);
		$this->sendHeaders($size);
		echo $document;
		exit;
	}
	
	function sendHeaders($size) {
		if (headers_sent()) {
			die('Headers already sent! The XML cannot be exported');
		}
		header('Content-type: text/xml');
		header('Cache-control: private');
		header('Expires: -1');
		header('Content-Length: ' . $size);
	}
	
}

?>